package com.webull.information.center.carwler.common.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

/**
 * 图片压缩服务
 *
 * @author smj
 * @version jdk1.8
 * @Description
 * @Date 下午4:27:56
 */
public class ThumbnailatorUtil {
    protected final static Logger logger = LogManager.getLogger(ThumbnailatorUtil.class);

//    public static NewsThumb converNewsThumb(byte[] imgBytes) {
//        return converNewsThumb(imgBytes, 200);
//    }

    /**
     * <pre>
     * 把图片文件传进入，生成统一规格的的新闻缩略结果对象
     * 包含规格文件byte[]和唯一name
     * 如果返回null,说明原始文件不合法或者失败
     * </pre>
     *
     * @param imgBytes 原始文件byte[]
     * @return
     * @throws IOException
     */
    public static NewsThumb converNewsThumb(byte[] imgBytes, int thumbSize) {

        final int minThumbSize = 50;
        final String fromat = "jpg";
        final int radioo = 3;
        try {
            BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imgBytes));

            if (originalImage.getWidth() <= minThumbSize || originalImage.getHeight() <= minThumbSize)// 图片太小忽略
                return null;

            if (originalImage.getWidth() >= (originalImage.getHeight() * radioo)// 长宽比例不正确
                    || originalImage.getHeight() >= (originalImage.getWidth() * radioo)) {
                return null;
            }

            int width_ = originalImage.getWidth() > thumbSize ? thumbSize : originalImage.getWidth();
            int high_ = originalImage.getHeight() > thumbSize ? thumbSize : originalImage.getHeight();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Builder<BufferedImage> build = Thumbnails.of(originalImage);//

            build = build.size(width_, high_);//
            build.outputFormat(fromat)//
                    .toOutputStream(os);

            NewsThumb thb = new NewsThumb();
            byte[] bytes = os.toByteArray();
            thb.setBytes(bytes);
            thb.setName("n_" + DigestUtils.md5Hex(bytes).substring(8, 24) + "." + fromat);
            return thb;
        } catch (Exception e) {
            logger.error("ThumbnailatorUtil.converNewsThumb error:{}", e);
            return null;
        }
    }

    public static class NewsThumb {
        private String name;
        private byte[] bytes;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public byte[] getBytes() {
            return bytes;
        }

        public void setBytes(byte[] bytes) {
            this.bytes = bytes;
        }

    }
}
